package team.sun.sss.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import team.sun.sss.common.domain.AjaxResult;
import team.sun.sss.common.domain.FileUploadUtils;
import team.sun.sss.common.domain.IOConfig;
import team.sun.sss.common.domain.TableDataInfo;
import team.sun.sss.common.entity.StudentEntity;
import team.sun.sss.common.service.StudentConService;

import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping("/common/man")
public class ManagementController  extends BaseController {
    private String baseUrl="management/";

    @Autowired
    private StudentConService studentService;
    @RequestMapping("/view")
    public String toUpdateFamily() {
        return baseUrl+"student";
    }
    /**
     * 查询学生列表
     */
    /**
     * 上传商品图片
     */
    @GetMapping("/avatar")
    public String avatar()
    {
        return baseUrl + "/avatar";
    }
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StudentEntity studentEntity)
    {
        startPage();
        QueryWrapper<StudentEntity> studentEntityQueryWrapper=new QueryWrapper<>();
        studentEntityQueryWrapper.eq("del_flag",0);
        if(studentEntity.getStuName()!=null&&!studentEntity.getStuName().equals(""))
        {
            studentEntityQueryWrapper.like("stu_name",studentEntity.getStuName());
        }
        List<StudentEntity> list = studentService.list(studentEntityQueryWrapper);
        return getDataTable(list);
    }

    /**
     * 新增学生信息
     */

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StudentEntity studentEntity)
    {
        return toAjax(studentService.save(studentEntity));
    }

    /**
     * 新增学生信息
     */
    @GetMapping("/add")
    public String add()
    {
        return baseUrl + "/add";
    }

    /**
     * 修改学生信息
     */
    @GetMapping("/edit/{stuId}")
    public String edit(@PathVariable("stuId") Integer stuId, ModelMap mmap)
    {
        mmap.put("stu", studentService.getById(stuId));
        return baseUrl + "/edit";
    }

    /**
     * 修改保存学生信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StudentEntity studentEntity)
    {
        return toAjax(studentService.updateById(studentEntity));
    }
    /**
     * 删除学生信息
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids[])
    {
        return toAjax(studentService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 保存头像
     */

    @PostMapping("/updateAvatar")
    @ResponseBody
    public AjaxResult updateAvatar(@RequestParam("avatarfile") MultipartFile file)
    {

        try
        {
            if (!file.isEmpty())
            {
                String avatar = FileUploadUtils.upload(IOConfig.getAvatarPath(), file);
                return success(avatar);
            }
            return error();
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}